<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-10-18
 * Time: 9:09
 */
namespace app\manage\admin;
use app\admin\controller\Admin;
use app\common\builder\ZBuilder; // 引入ZBuilder
use app\common\model\News as NewsModel;
use app\common\model\Newscat;
use think\Db;
use think\App;

class News extends Admin
{
    public $cats='';
    //文章的分类
    public function __construct(App $app = null){
        $this->cats=Newscat::column('name','id');
        parent::__construct($app);
    }
    //文章列表
    public function news_list($group = '1')
    {

        $list_tab = [
            '1' => ['title' => '新闻列表', 'url' => url('news_list', ['group' => '1'])],
            '2' => ['title' => '系统文章', 'url' => url('news_list', ['group' => '2'])],
        ];
        // 读取数据
        if($group == '1'){
            $map['nc.type'] = '2';
        }else{
            $map['nc.type'] = '1';
        }
        // 排序
        $order = $this->getOrder();
        // 数据列表
        $data_list = Db::name('qt_news')
            ->field('n.*,nc.name')
            ->alias('n')
            ->leftJoin('qt_news_category nc','n.category_id = nc.id')
            ->where($map)->order($order)
            ->paginate();

        // 分页数据
        $page = $data_list->render();
        $btn_access1 = [
            'title' => '文章编辑',
            'icon'  => 'fa fa-fw fa-pencil',
            'class' => 'btn btn-xs btn-default',
            'href'  => url('edit', ['id' => '__id__']),
        ];
        $btn_access = [
            'title' => '新增文章',
            'icon'  => 'fa fa-fw fa-plus',
            'class' => 'btn btn-info',
            'href'  => url('add',['group'=>$group]),
        ];
        $btn_access2 = [
            'title' => '删除文章',
            'icon'  => 'fa fa-fw fa-trash-o',
            'class' => 'btn btn-xs btn-default ajax-get confirm',
            'href'  => url('delete', ['id' => '__id__']),
        ];
        //设置页面
        return ZBuilder::make('table')
            ->hideCheckbox()
            ->addColumn('id', '编号', 'text')
            ->addColumn('category_id', '分类', 'status','',$this->cats)
            ->addColumn('title', '标题', 'text')
            ->addColumn('image','导图','picture')
            ->addColumn('right_button','操作','btn')
            ->setTableName('qt_news') // 指定数据表名
            ->addTopButton('custom',$btn_access) // 添加顶部按钮
            ->addRightButton('edit',$btn_access1) // 添加右侧按钮
            ->addRightButton('delete',$btn_access2) // 添加右侧按钮
            ->replaceRightButton(['type' => ['neq', '2']], '<button class="btn btn-danger btn-xs" type="button" disabled>不可操作</button>', ['delete'])
            ->setPrimaryKey('id') // 设置主键名为username
            ->setPageTitle('文章列表')
            ->setRowList($data_list) // 设置表格数据
            ->setPages($page) // 设置分页数据
            ->setTabNav($list_tab,  $group)
            ->fetch();
    }
    //添加文章
    public function add($group = ''){
        //判断是否提交信息
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            $result = $this->validate($data, 'News');
            if(true !== $result) $this->error($result);
            if($group == '2'){
                $find = Db::name('qt_news')->where('category_id',$data['category_id'])->find();
                if($find){
                    $this->error('系统分类下面只能添加一个');
                }
            }
            if (NewsModel::create($data)) {
                $this->success('新增成功', 'news_list');
            } else {
                $this->error('新增失败');
            }
        }else
        {
            if($group == '1'){
                $this->cats=Newscat::where('type','2')->column('name','id');
            }else{
                $this->cats=Newscat::where('type','1')->column('name','id');
            }

            return ZBuilder::make('form')
                ->addFormItems(
                    [
                        ['text:7','title','标题'],
                        ['select:7','category_id','分类','<code>当分类为系统分类时，禁止修改</code>',$this->cats],
                        ['image:7','image','导图'],
                        ['Ueditor:7','content','内容']
                    ]
                )
                ->setPageTitle('添加文章')
                ->fetch();
        }
    }
    //编辑公告
    public function edit($id=null){
        //判断是否提交信息
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'News');
            if(true !== $result) $this->error($result);

            if (NewsModel::update($data)) {
                $this->success('编辑成功', 'news_list');
            } else {
                $this->error('编辑失败');
            }
        }else
        {
            $info = NewsModel::get($id);
            return ZBuilder::make('form')
            ->addFormItems(
                [
                    ['hidden','id','id'],
                    ['text:7','title','标题'],
                    ['select:7','category_id','分类','<code>系统分类禁止修改</code>',$this->cats],
                    ['image:7','image','导图'],
                    ['Ueditor:7','content','公告内容'],
                ]
            )
            ->setFormData($info)
            ->setPageTitle('编辑文章')
            ->fetch();
        }
    }

    //删除文章
    public function delete($id=null){
        if ($id === null) $this->error('缺少参数');
        $re=NewsModel::destroy($id);// 删除主键为1,2,3的数据
        if($re){
            $this->success('删除成功','news_list');
        }else{
            $this->error('系统繁忙请稍后再试','news_list');
        }

    }



}